package com.example.words;

import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MyAdapter extends ListAdapter<Word,MyAdapter.MyViewHolder> {   //ListAdapter会在后台对提交的列表数据进行差异比较
//    private List<Word> allWords = new ArrayList<>();
    boolean useCardView;
    private WordViewModel wordViewModel;

    public MyAdapter(boolean useCardView,WordViewModel wordViewModel) {
        super(new DiffUtil.ItemCallback<Word>() {  //列表差异化处理的一个回调   列表数据的差异化处理实在后台异步进行的
            @Override
            public boolean areItemsTheSame(@NonNull Word oldItem, @NonNull Word newItem) {   //比较列表中的两个元素是否相同
                return oldItem.getId() == newItem.getId();
            }

            @Override
            public boolean areContentsTheSame(@NonNull Word oldItem, @NonNull Word newItem) {   //比较列表中的两个内容是否相同
                return (oldItem.getWord().equals(newItem.getWord()))
                        && oldItem.getChineseMeaning().equals(newItem.getChineseMeaning())
                        && oldItem.isChineseInvisible() == newItem.isChineseInvisible();
            }
        });
        this.useCardView = useCardView;
        this.wordViewModel = wordViewModel;
    }

//    public void setAllWords(List<Word> allWords) {
//        this.allWords = allWords;
//    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {    //创建ViewHolder时呼叫   一行就是一个ViewHolder
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View itemView;
        if(useCardView){
            itemView = layoutInflater.inflate(R.layout.cell_card_2,parent,false);
        }else {
            itemView = layoutInflater.inflate(R.layout.cell_normal_2,parent,false);
        }
        final MyViewHolder holder = new MyViewHolder(itemView);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("https://m.youdao.com/dict?le=eng&q="+holder.textViewEnglish.getText());
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(uri);
                holder.itemView.getContext().startActivity(intent);
            }
        });

        holder.aSwitchChineseInvisible.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Word word = (Word)holder.itemView.getTag(R.id.word_for_view_holder);
                if (isChecked){
                    holder.textViewChinese.setVisibility(View.GONE);
                    word.setChineseInvisible(true);
                    wordViewModel.updateWords(word);
                }else {
                    holder.textViewChinese.setVisibility(View.VISIBLE);
                    word.setChineseInvisible(false);
                    wordViewModel.updateWords(word);
                }
            }
        });
        return holder;
        //return null;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {   //ViewHolder和RecyclerView绑定时呼叫
        Word word = getItem(position);
        holder.itemView.setTag(R.id.word_for_view_holder,word);       //setTag()中可以放任意一个对象，在其他地方可以用getTag()获取调用
        holder.textViewNum.setText(String.valueOf(position+1));
        holder.textViewEnglish.setText(word.getWord());
        holder.textViewChinese.setText(word.getChineseMeaning());
        //holder.aSwitchChineseInvisible.setOnCheckedChangeListener(null);  //先清空，然后赋值，然后监听   改进后的代码在生成的时候就绑定了listener，所以这里删掉
        if (word.isChineseInvisible()){
            holder.textViewChinese.setVisibility(View.GONE);    //setVisibility有三个参数：VISIBLE  INVISIBLE  GONE      GONE：不显示，位置也没有   INVISIBLE：不显示，但是会占据位置
            holder.aSwitchChineseInvisible.setChecked(true);
        }else {
            holder.textViewChinese.setVisibility(View.VISIBLE);
            holder.aSwitchChineseInvisible.setChecked(false);
        }
    }

//    @Override
//    public int getItemCount() {    //返回列表总个数
//        return allWords.size();
//    }


    @Override
    public void onViewAttachedToWindow(@NonNull MyViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        holder.textViewNum.setText(String.valueOf(holder.getAdapterPosition()+1));
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView textViewNum,textViewEnglish,textViewChinese;
        Switch aSwitchChineseInvisible;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textViewNum = itemView.findViewById(R.id.textViewNum);
            textViewEnglish = itemView.findViewById(R.id.textViewEnglish);
            textViewChinese = itemView.findViewById(R.id.textViewChinese);
            aSwitchChineseInvisible = itemView.findViewById(R.id.switchChineseInvisible);
        }
    }
}
